package com.miya.demo.controller;

import com.miya.demo.common.util.EsApiUtil;
import com.miya.demo.model.base.BaseResponse;
import com.miya.demo.model.dto.system.LoginDTO;
import com.miya.demo.model.dto.system.RegisterDTO;
import com.miya.demo.model.vo.LoginVO;
import com.miya.demo.service.LoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制器
 *
 * @author Caixiaowei
 * @date 2022/07/08
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录API")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 注册
     *
     * @param dto dto
     * @return {@link BaseResponse<Void>}
     */
    @PostMapping("/register")
    public BaseResponse<Void> register (@RequestBody @Validated RegisterDTO dto) {
        loginService.register(EsApiUtil.encodeForHtml(dto));
        return BaseResponse.create();
    }

    /**
     * 登录
     *
     * @param dto dto
     * @return {@link BaseResponse<LoginVO>}
     */
    @PostMapping
    public BaseResponse<LoginVO> login (@RequestBody @Validated LoginDTO dto) {
        LoginVO loginVO = loginService.login(dto);
        return BaseResponse.create(loginVO);
    }
}
